import requests
import random
import os

# 替换为你的VirusShare API密钥
api_key = 'your_api_key'

# 替换为你希望下载的样本数量
num_samples = 10000

# 替换为保存下载文件的目录
download_directory = '/path/to/download/directory/'

# VirusShare API的基本URL
base_url = 'https://virusshare.com/apiv2/'

# 获取样本列表
response = requests.get(f'{base_url}get_info', params={'apikey': api_key})
sample_list = response.json()

# 从样本列表中随机选择指定数量的样本
selected_samples = random.sample(sample_list, num_samples)

# 创建保存样本的目录
os.makedirs(download_directory, exist_ok=True)

# 下载样本
for sample in selected_samples:
    sample_id = sample['sha256']
    download_url = f'{base_url}download?apikey={api_key}&hash={sample_id}'
    file_path = os.path.join(download_directory, f'{sample_id}.zip')

    with open(file_path, 'wb') as file:
        sample_data = requests.get(download_url).content
        file.write(sample_data)

    print(f'Downloaded: {file_path}')
